var util = require("../../utils/util")
Component({
  properties: {
    cartArr: {
      type: Array,
    },
  },

  data: {
    cart_show: false,
    checkedAll: true,
    checkNum: '',
    cartTotalPrice: '0.00'
  },
  observers: {
    'cartArr': function () {
      this.getTotalPrice();
      //计算cart选中项
      var count = 0
      for (let i in this.data.cartArr) {
        // console.log(this.data.cartArr[i].checked)
        if (this.data.cartArr[i].checked) {
          count++
        } else {
          break
        }
      }
      this.setData({
        checkNum: count
      })
    }
  },

  ready() {
    var app = getApp()
    this.setData({
      navHeight: app.globalData.navHeight,
      urls: app.globalData.urls
    })
    this.getTotalPrice()
  },

  methods: {
    toBookOrder() {
      wx.navigateTo({
        url: '/pages/Cashier/bookOrder/bookOrder',
      })
    },
    toAppointOrder() {
      wx.navigateTo({
        url: '/pages/Cashier/appointOrder/appointOrder',
      })
    },
    
    saveCheck() {
      //修改check之后，将购物车信息保存到全局
      getApp().globalData.cartData = this.data.cartArr
    },
    showCartPop() {
      this.setData({
        cart_show: !this.data.cart_show,
      })
    },
    closePop() {
      this.setData({
        cart_show: false
      })
    },
    checkAll() {
      if (this.data.checkedAll) {
        for (let i in this.data.cartArr) {
          this.setData({
            [`cartArr[${i}].checked`]: false,
            checkedAll: false,
            checkNum: 0
          })
        }
      } else {
        for (let i in this.data.cartArr) {
          this.setData({
            [`cartArr[${i}].checked`]: true,
            checkedAll: true,
            checkNum: this.data.cartArr.length
          })
        }
      }
      this.getTotalPrice();
      this.saveCheck()
    },
    editCart(e) {
      // console.log(e.currentTarget.dataset.item)
      // let goods_id = e.currentTarget.dataset.item.goods_id
      this.triggerEvent('editCart', e)
    },
    onCheck(e) {
      let index = e.currentTarget.dataset.index
      // console.log(this.data.cartArr[index].checked)
      this.setData({
        [`cartArr[${index}].checked`]: !this.data.cartArr[index].checked
      })
      //判断是否所有的按钮都被check了
      var count = 0
      for (let i in this.data.cartArr) {
        // console.log(this.data.cartArr[i].checked)
        if (this.data.cartArr[i].checked) {
          count++
        } else {
          break
        }
      }
      this.setData({
        checkNum: count
      })
      if (count == this.data.cartArr.length) {
        this.setData({
          checkedAll: false,
        })
        this.checkAll()
      } else if (count === 0) {
        this.setData({
          checkedAll: true,
        })
        this.checkAll()
      } else {
        this.getTotalPrice();
        this.saveCheck()
      }
    },
    //计算购物车内总价
    getTotalPrice() {
      let result = 0
      let cartArr = this.data.cartArr
      for (let i in cartArr) {
        if (cartArr[i].checked) {
          result += cartArr[i].price * cartArr[i].goods_num
        }
      }
      // console.log(result)
      this.setData({
        cartTotalPrice: result.toFixed(2)
      })
    },
    cleanCart() {
      // console.log(this.data.cartArr)
      //判断 哪些被选中了， 添加到data 中
      var data = {}
      var deletGoodArr = []
      var cartArr = this.data.cartArr
      for (let i in cartArr) {
        if (cartArr[i].checked) {
          data['car_id[' + i + ']'] = cartArr[i].car_id;
          //记录对应商品的信息，用于后续对goodArr操作，设置数量为0
          deletGoodArr.push(cartArr[i])
        }
      }
      // console.log(deletGoodArr)
      //判断结果是否为空
      if (!(Object.keys(data).length == 0)) {
        util.sendRequest('goods/cartdelete', 'POST', data).then(res => {
          // console.log(res)
          if (res.data.code === 0) {
            //获取最新的购物车信息
            this.triggerEvent('deleteCart', deletGoodArr)
            wx.showToast({
              title: '已删除选中商品',
              icon: 'none'
            })
          }
        })
      } else {
        wx.showToast({
          title: '未选择商品',
          icon: 'none'
        })
      }
    }
  },
})